package jun.empdepmanagersystem02.controller;

import jun.empdepmanagersystem02.pojo.Emp;
import jun.empdepmanagersystem02.pojo.Result;
import jun.empdepmanagersystem02.service.EmpService;
import jun.empdepmanagersystem02.until.JWTuntils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
public class LoginController {
    @Autowired
    private EmpService empService;

    @PostMapping("/login")
    public Result login(@RequestBody Emp emp) {
        if (empService.getByUsername(emp.getUsername()) != null && empService.getByUsername(emp.getUsername()).getPassword().equals(emp.getPassword())) {
            Map<String, String> msg = new HashMap();
            msg.put("username", emp.getUsername());
            return Result.success(JWTuntils.getToken(msg));
        } else {
            return Result.error("登录失败 账户或者密码错误");
        }


    }

}
